# 登录注册功能测试脚本

# 应用启动信息
$baseUrl = "http://localhost:5153"
$apiPrefix = "/api"

Write-Host "======================================" -ForegroundColor Green
Write-Host "🚀 登录注册功能测试" -ForegroundColor Green  
Write-Host "======================================" -ForegroundColor Green
Write-Host "应用地址: $baseUrl" -ForegroundColor Cyan
Write-Host "API文档: $baseUrl/swagger" -ForegroundColor Cyan
Write-Host ""

# 等待应用启动
Write-Host "⏳ 正在等待应用启动..." -ForegroundColor Yellow

$maxRetries = 30
$retryCount = 0
$isRunning = $false

while ($retryCount -lt $maxRetries -and -not $isRunning) {
    try {
        $response = Invoke-WebRequest -Uri "$baseUrl/swagger" -Method GET -TimeoutSec 5 -ErrorAction SilentlyContinue
        if ($response.StatusCode -eq 200) {
            $isRunning = $true
            Write-Host "✅ 应用启动成功!" -ForegroundColor Green
        }
    }
    catch {
        $retryCount++
        Write-Host "." -NoNewline -ForegroundColor Yellow
        Start-Sleep -Seconds 2
    }
}

if (-not $isRunning) {
    Write-Host ""
    Write-Host "❌ 应用启动超时，请检查终端输出" -ForegroundColor Red
    Write-Host "请手动访问: $baseUrl/swagger" -ForegroundColor Yellow
    exit 1
}

Write-Host ""
Write-Host "🌐 可用的API端点:" -ForegroundColor Blue
Write-Host "  📍 POST $baseUrl/api/auth/register - 用户注册"
Write-Host "  📍 POST $baseUrl/api/auth/login - 用户登录"
Write-Host "  📍 POST $baseUrl/api/auth/refresh-token - 刷新令牌"
Write-Host "  📍 POST $baseUrl/api/auth/send-sms-code - 发送短信验证码"
Write-Host "  📍 POST $baseUrl/api/auth/reset-password - 重置密码"

Write-Host ""
Write-Host "📋 测试用户注册:" -ForegroundColor Magenta

$registerData = @{
    Username = "testuser$(Get-Date -Format 'HHmmss')"
    Email = "testuser$(Get-Date -Format 'HHmmss')@example.com"  
    Password = "Test123!@#"
    Phone = "13800138000"
} | ConvertTo-Json

Write-Host "注册数据: $registerData" -ForegroundColor Gray

try {
    $registerResponse = Invoke-RestMethod -Uri "$baseUrl/api/auth/register" -Method POST -Body $registerData -ContentType "application/json"
    Write-Host "✅ 注册成功: $($registerResponse.Message)" -ForegroundColor Green
    $testUsername = ($registerData | ConvertFrom-Json).Username
} catch {
    Write-Host "❌ 注册失败: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "💡 可能是服务未完全启动或数据库连接问题" -ForegroundColor Yellow
    $testUsername = "admin" # 使用默认用户测试登录
}

Write-Host ""
Write-Host "🔐 测试用户登录:" -ForegroundColor Magenta

$loginData = @{
    Username = $testUsername
    Password = "Test123!@#"
} | ConvertTo-Json

Write-Host "登录数据: $loginData" -ForegroundColor Gray

try {
    $loginResponse = Invoke-RestMethod -Uri "$baseUrl/api/auth/login" -Method POST -Body $loginData -ContentType "application/json"
    Write-Host "✅ 登录成功!" -ForegroundColor Green
    Write-Host "访问令牌: $($loginResponse.accessToken.Substring(0, 20))..." -ForegroundColor Green
    Write-Host "刷新令牌: $($loginResponse.refreshToken.Substring(0, 20))..." -ForegroundColor Green
} catch {
    Write-Host "❌ 登录失败: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "💡 可能需要使用系统中已存在的用户账号" -ForegroundColor Yellow
}

Write-Host ""
Write-Host "🎯 测试配置迁移API:" -ForegroundColor Cyan

try {
    $configStatus = Invoke-RestMethod -Uri "$baseUrl/api/ConfigurationMigration/status" -Method GET
    Write-Host "✅ 配置状态获取成功:" -ForegroundColor Green
    Write-Host "  - 使用集中化配置: $($configStatus.IsUsingCentralizedConfig)" -ForegroundColor Gray
    Write-Host "  - 配置提供商: $($configStatus.ConfigProvider)" -ForegroundColor Gray
} catch {
    Write-Host "❌ 配置状态获取失败: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host ""
Write-Host "🌍 浏览器访问链接:" -ForegroundColor Yellow
Write-Host "  🔗 Swagger API文档: $baseUrl/swagger"
Write-Host "  🔗 应用首页: $baseUrl"

Write-Host ""
Write-Host "✅ 测试完成！" -ForegroundColor Green
Write-Host "现在您可以通过浏览器或Postman进一步测试API功能" -ForegroundColor White
